package com.admission.web.admin;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Namespace;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.ServletUtils;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.PayLog;
import com.admission.service.PayLogManager;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;

@Namespace("/admin")
public class PayLogAdminAction extends CrudActionSupport<PayLog> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8054427664625941901L;
	private Page<PayLog> page = new Page<PayLog>(PageUtils.DEFAULT_PAGE_SIZE);
	private PayLogManager payLogManager;
	private String id;
	private PayLog entity;
	private String payStatus;
	private String status;

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public void setPayStatus(String payStatus) {
		this.payStatus = payStatus;
	}

	public PayLog getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		page = payLogManager.getPageByFilter(page, filters);

		return "pay-log-admin";
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		entity = payLogManager.getPayLogById(id);
		return "detail";
	}

	
	/**
	 * 改变开放状态
	 * 
	 * @return
	 * @throws Exception
	 */
	public String changeConsult() throws Exception {
		payLogManager.changeConsult(status);
		if (status.equals("1"))
			Struts2Utils.renderText("true");
		else
			Struts2Utils.renderText("false");
		return null;
	}
	
	public String excelOut() throws Exception {
		List<PayLog> list;
		if (!StringUtils.isEmpty(payStatus))
			list = payLogManager.getAllByStatus(payStatus);
		else
			list = payLogManager.getAll();

		PayLog payLog = null;

		XSSFWorkbook xwb = new XSSFWorkbook();
		CreationHelper createHelper = xwb.getCreationHelper();

		Sheet s = xwb.createSheet();
		CellStyle cs = xwb.createCellStyle();
		CellStyle cs2 = xwb.createCellStyle();
		DataFormat df = xwb.createDataFormat();

		Font f = xwb.createFont();
		Font f2 = xwb.createFont();

		f.setFontHeightInPoints((short) 12);
		f.setColor(IndexedColors.RED.getIndex());
		f.setBoldweight(Font.BOLDWEIGHT_BOLD);

		f2.setFontHeightInPoints((short) 10);
		f2.setColor(IndexedColors.RED.getIndex());
		f2.setBoldweight(Font.BOLDWEIGHT_BOLD);

		cs.setFont(f);
		cs.setDataFormat(df.getFormat("#,##0.0"));

		cs2.setBorderBottom(cs2.BORDER_THIN);
		cs2.setDataFormat(df.getFormat("text"));
		cs2.setFont(f2);

		Row r = s.createRow(0);
		Cell c00 = r.createCell(0);
		Cell c01 = r.createCell(1);
		Cell c02 = r.createCell(2);
		Cell c03 = r.createCell(3);
		Cell c04 = r.createCell(4);
		Cell c05 = r.createCell(5);
		Cell c06 = r.createCell(6);
		Cell c07 = r.createCell(7);
		Cell c08 = r.createCell(8);
		Cell c09 = r.createCell(9);
		Cell c10 = r.createCell(10);

		c00.setCellValue("学生号");
		c01.setCellValue("身份证号");
		c02.setCellValue("姓名");
		c03.setCellValue("性别");
		c04.setCellValue("报名点名称");
		c05.setCellValue("缴费金额");
		c06.setCellValue("缴费状态");
		c07.setCellValue("缴费日期");
		c08.setCellValue("联系方式");
		c09.setCellValue("组别");
		c10.setCellValue("教室名称");

		for (int rownum = 0; rownum < list.size(); rownum++) {
			payLog = list.get(rownum);
			r = s.createRow(rownum + 1);
			c00 = r.createCell(0);
			c01 = r.createCell(1);
			c02 = r.createCell(2);
			c03 = r.createCell(3);
			c04 = r.createCell(4);
			c05 = r.createCell(5);
			c06 = r.createCell(6);
			c07 = r.createCell(7);
			c08 = r.createCell(8);
			c09 = r.createCell(9);
			c10 = r.createCell(10);

			c00.setCellValue(payLog.getExamieeNo());
			c01.setCellValue(payLog.getIdnum());
			c02.setCellValue(payLog.getName());
			c03.setCellValue(payLog.getGender());
			c04.setCellValue(payLog.getSignPlace());
			c05.setCellValue(payLog.getAmount());
			if (payLog.getPayStatus().equals("1"))
				c06.setCellValue("已缴费");
			else
				c06.setCellValue("未缴费");
			// c06.setCellValue(payLog.getPayStatus());
			c07.setCellValue(payLog.getPayTime());
			c08.setCellValue(payLog.getMobile());
			c09.setCellValue(payLog.getGroupName());
			c10.setCellValue(payLog.getClassroom());
		}

		// 输出Excel文件.
		HttpServletResponse response = Struts2Utils.getResponse();
		response.setContentType(ServletUtils.EXCEL_TYPE);
		ServletUtils.setFileDownloadHeader(response, "缴费统计列表.xlsx");

		xwb.write(response.getOutputStream());
		response.getOutputStream().flush();
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public String save() throws Exception {
		try {
			payLogManager.save(entity);
		} catch (Exception e) {
			this.addActionMessage("操作失败，请重新操作");
		}
		return this.list();
	}


	public Page<PayLog> getPage() {
		return page;
	}

	public void setPage(Page<PayLog> page) {
		this.page = page;
	}

	public PayLogManager getPayLogManager() {
		return payLogManager;
	}

	public void setPayLogManager(PayLogManager payLogManager) {
		this.payLogManager = payLogManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public PayLog getEntity() {
		return entity;
	}

	public void setEntity(PayLog entity) {
		this.entity = entity;
	}

}
